package top.infopub.mgr.bpa.service.impl;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.infopub.dto.bpa.DeviceEntryDto;
import top.infopub.dto.bpa.DeviceSuppleDto;
import top.infopub.enums.mkt.ProjectFileTypeEnum;
import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.bpa.dao.BpaContractBpaReviewDao;
import top.infopub.mgr.bpa.dao.DeviceEntryDao;
import top.infopub.mgr.bpa.dao.DeviceSuppleDao;
import top.infopub.mgr.bpa.domain.DeviceEntryDomain;
import top.infopub.mgr.bpa.service.DeviceEntryService;
import top.infopub.mgr.bpa.vo.DeviceEntryVo;
import top.infopub.model.bpa.*;
import top.infopub.model.mkt.ProjectAttachment;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import java.text.ParseException;
import java.util.Date;
import java.util.List;


@Service
public class DeviceEntryServiceImpl extends GenericServiceImpl<DeviceEntry, String> implements DeviceEntryService{

    @Autowired
    private DeviceEntryDao deviceEntryDao;

    @Autowired
    private BpaContractBpaReviewDao bpaContractBpaReviewDao;

    @Autowired
    private DeviceSuppleDao deviceSuppleDao;


    @Override
    public GenericDao<DeviceEntry, String> getDao() {
        return deviceEntryDao;
    }


    @Override
    public Page<DeviceEntryDomain> selectByDomainWhere(Page<DeviceEntryDomain> page,
                                                       DeviceEntryVo deviceEntryVo) throws ParseException {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<DeviceEntryDomain>)deviceEntryDao.selectByDomainWhere(deviceEntryVo);
    }

    @Override
    public Basic projectBasic(String proCode) {
        return deviceSuppleDao.findByProCode(proCode);
    }

    @Override
    public DeviceEntry findById(String id) {
        return deviceEntryDao.findById(id);
    }

    @Override
    public int save(DeviceEntryDto dto) {
        //登录人员信息
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        DeviceEntry deviceEntry = dto.getDeviceEntry();
        String id = deviceEntry.getId();
        if (StringUtils.isEmpty(id)) {
            //第一次 保存
            deviceEntry.setAddBy(vo.getId());
            deviceEntry.setAddTime(new Date());
            deviceEntry.setProcessStatus("0");
            //保存主表数据
            deviceEntryDao.insertDeviceEntry(deviceEntry);
        }else {
            //修改
            deviceEntry.setUptBy(vo.getId());
            deviceEntry.setUptTime(new Date());
            deviceEntryDao.updateDeviceEntry(deviceEntry);
        }
        //删除合同清单录入数据
        bpaContractBpaReviewDao.deleteListing(id,"8");
        //保存合同清单录入数据
        int seq2 = 0;
        List<BpaContractListing> bpaContractListingList = dto.getBpaContractListingList();
        for (BpaContractListing bpaContractListing : bpaContractListingList) {
            bpaContractListing.setType("8");
            bpaContractListing.setContractId(id);
            bpaContractListing.setSeq(seq2++);
        }
        if (bpaContractListingList!=null){
            bpaContractBpaReviewDao.insertBatchListing(bpaContractListingList);
        }
        return 1;

    }

    @Override
    public DeviceEntryDto queryById(String id) {

        DeviceEntryDto dto = new DeviceEntryDto();
        //根据id查询主表数据
        DeviceSupple deviceSupple = deviceSuppleDao.findById(id);
        //根据主表数据的proCode查询 项目基本信息
        String proCode = deviceSupple.getProCode();
        if (!StringUtils.isEmpty(proCode)) {
            Basic basic = deviceSuppleDao.findByProCode(proCode);
            // String projectLeader = otherContractsReview.getProjectLeader();
            //if (!StringUtils.isEmpty(projectLeader)){
            //basic.setProjectLeader(projectLeader);
            //}
            dto.setBasic(basic);
        }
        //合同清单信息  设备合同清单录入
        List<BpaContractListing> list = bpaContractBpaReviewDao.selectListing(id, "8");
        dto.setBpaContractListingList(list);
        return dto;
    }

}